<!DOCTYPE html>
<html>

<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="../js/plugins/jquery-form/jquery.form.js"></script>
    <script src="../js/plugins/eleditor/Eleditor.min.js"></script>
    <script src="../js/plugins/eleditor/webuploader.min.js"></script>
    <script src="../js/plugins/My97DatePicker/WdatePicker.js"></script>
    <script src="../js/common.js"></script>
    <link rel="stylesheet" href="/js/plugins/chosen/component-chosen.css"/>
    <script src="/js/plugins/chosen/chosen.jquery.js"></script>


    <script>
        $(function () {
            //发送异步请求获取旅游地点的数据
            $.get("/regions", function (data) {
                var html = "";
                //遍历JSON数据拼接到旅游地点下拉框中
                $.each(data, function (index, ele) {
                    html += "<option value='" + ele.id + "'>" + ele.name + "</option>"
                })

                $("#regions").html(html);
                //对下拉框使用JqueryChosen插件
                $("#regions").chosen();

                //编辑的回显操作 获取浏览器地址上?后面的参数
                var params = getParams();
                if (params.id){
                    //发送请求获取游记的参数  根据id获取哪一篇的游记
                    $.get("/travels/"+params.id,function (data) {
                        if(data.success){
                            console.log(data.data);
                            //获取到游记数据后根据模板进行回显操作
                            $(".travels").renderValues(data.data);

                            //修改表单为PUT表单
                            $("#_method").val("PUT");

                        }
                    })
                }
            })

            //游记点击更换封面按钮
            $("#changeBtn").click(function () {
                $("#imgInput").click();
                //绑定值改变事件
                $("#imgInput").change(function () {
                    //当有传入图片发送异步请求 上传图片 回显图片数据
                    if (this.value) {
                        $("#coverForm").ajaxSubmit(function (data) {
                            if (data.status == 1) {
                                $("#coverUrl").attr("src", data.url);
                            }
                        })
                    }
                })
            })

            //移动富文本框插件
            var Edr = new Eleditor({
                el: '#contentEditor',
                upload: {
                    //图片上传的服务器接口
                    server: '/images',
                    //修改图片接收参数的名称
                    formName: 'pic'
                },
            });


            //游记保存事件绑定
            $("#save").click(function () {

                var state = $(this).data("state");

                $("#detailForm").ajaxSubmit({
                        //表单提交前执行的函数
                        //params表单参数数组-----Form表单中相关的参数封装成数组对象
                        //Jquery的form表单
                        //options表单的详细配置
                        beforeSubmit: function (params, form, options) {
                            //在表单提交之前 将缺少的的参数添加到参数列表中
                            params.push({name: "title", value: $("#title").val()})
                            params.push({name: "coverUrl", value: $("#coverUrl").attr("src")})
                            params.push({name: "state", value: state})
                            params.push({name: "travelContent.content", value: Edr.getContent()})
                        },
                        success:function (data) {
                            if(data.success){
                                //当添加操作完成之后跳到我的游记页面
                                window.location.href="/mine/travels.html";
                            }
                        }
                    }
                )
            })
            //游记的发布操作
            $("#release").click(function () {

                var state = $(this).data("state");

                $("#detailForm").ajaxSubmit({
                        //表单提交前执行的函数
                        //params表单参数数组-----Form表单中相关的参数封装成数组对象
                        //Jquery的form表单
                        //options表单的详细配置
                        beforeSubmit: function (params, form, options) {
                            //在表单提交之前 将缺少的的参数添加到参数列表中
                            params.push({name: "title", value: $("#title").val()})
                            params.push({name: "coverUrl", value: $("#coverUrl").attr("src")})
                            params.push({name: "state", value: state})
                            params.push({name: "travelContent.content", value: Edr.getContent()})
                        },
                        success:function (data) {
                            if(data.success){
                                //当添加操作完成之后跳到我的游记页面
                                window.location.href="/mine/travels.html";
                            }
                        }
                    }
                )
            })


        })
    </script>


</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-floppy-o fg-fw" id="save" data-state="1"><span>保存</span></i>
            <i class="fa fa-check" id="release" data-state="2"><span>发布</span></i>
        </div>
    </div>
</div>

<div class="travels form-group">
    <div class="cover">
        <img id="coverUrl" width="100%" height="200px" render-src="coverUrl"/>
        <form id="coverForm" method="post" enctype="multipart/form-data" action="/images">
            <input id="imgInput" type="file" name="pic" style="display: none"
                   accept="image/gif,image/jpeg,image/jpg,image/png"/>
            <button type="button" class="btn" id="changeBtn">
                <i class="fa fa-pencil-square-o"></i>
                <small>更换封面</small>
            </button>
        </form>

    </div>
    <div class="title">
        <input class="form-control" id="title" placeholder="请输入标题" render-value="title"/>
    </div>
    <h6 href="#">点击文章内容即可编辑</h6>
    <!--Eleditor关联的元素-->
    <div id="contentEditor" render-html="travelContent.content">
    </div>

    <hr/>

    <div class="container">
        <form id="detailForm" method="post" action="/travels">
            <input type="hidden" name="id" render-value="id"/>
            <!--需要判断如果是编辑 需要在隐藏input元素上加上PUT的VALUE-->
            <input type="hidden" id="_method" name="_method"/>

            <label>出行时间:</label>
            <input type="text" name="travelTime" render-value="travelTime" class="form-control" onclick="WdatePicker()">
            <label>出行天数:</label>
            <input type="number" name="days" render-value="days" class="form-control">
            <label>参与人物:</label>
            <!--这里回显需要特殊处理-->
            <select class="c-select form-control" name="person" render-value="person">
                <option value="1" selected>一个人的旅行</option>
                <option value="2">和父母</option>
                <option value="3">和朋友</option>
                <option value="4">和同事</option>
                <option value="5">和爱人</option>
                <option value="6">和其他</option>
            </select>
            <label>人均费用:</label>
            <input type="text" name="perExpends" render-value="perExpends" class="form-control">

            <label>旅游地点:</label>
            <select class="form-control" id="regions" name="place.id" render-value="place.id">

            </select>

            <div class="form-check">
                <label class="form-check-label">
                    <!--设置自定义属性:属性名等号左边值 属性值 等号右边 最终结果就是checked = isPublic所对应的值-->
                    <input class="form-check-input" type="checkbox" name="isPublic" render-attr="checked=isPublic">是否公开
                </label>
            </div>

        </form>

    </div>

    <hr/>

</div>


</body>

</html>